package com.cl.photozoom.ui;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.cl.photozoom.PhotoView;
import com.cl.photozoom.PhotoZoomInstance;

import java.util.ArrayList;


/**
 * PhotoPagerAdapter
 * Created by Hasee on 2018/5/12.
 */
public class PhotoPagerAdapter extends PagerAdapter {
    private final ArrayList<PhotoViewInterface> mlist;
    private final PhotoActivity mContext;

    public PhotoPagerAdapter(ArrayList<PhotoViewInterface> picarr, PhotoActivity context) {
        this.mlist = picarr;
        this.mContext = context;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {

        final PhotoView imageView = new PhotoView(mContext);
        if (PhotoZoomInstance.mImageEngine != null) {
            PhotoZoomInstance.mImageEngine.loadImage(mContext, mlist.get(position).getPicUrl(), imageView);
        }

        container.addView(imageView);

        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
}
